package view;

import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import javax.swing.JButton;

import javax.swing.WindowConstants;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.SwingConstants;

import model.Family;

import control.CConnection;
import control.CFamily;

/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
public class JListFamily extends javax.swing.JFrame {
	private JLabel lblTitle;
	private JScrollPane jScrollPane1;
	private JButton bttEdit;
	private JButton bttDelete;
	private JTable tbFamilies;
	private DefaultTableModel tbFamiliesModel;

	private ArrayList<Family> families;
	/**
	* Auto-generated main method to display this 
	* JPanel inside a new JFrame.
	*/
		
	public JListFamily() {
		super();
		initGUI();

		this.loadFamilies();
		
		bttDelete.addActionListener(new ActionListener() 
		{	
			@Override
			public void actionPerformed(ActionEvent arg0) {
				deleteFamily();
			}
		});
		
		bttEdit.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent arg0) {
				editFamily();
			}
		});
	}
	
	private void cleanList()
	{
		while(tbFamilies.getRowCount()>0)
			tbFamiliesModel.removeRow(0);
	}
	
	public void loadFamilies()
	{
		this.cleanList();
		try {
			families = CFamily.getInstance().retrieveAll();
			for (Family family : families) {
				tbFamiliesModel.addRow(new Object[]{family.getName(), family.getDescription(), family.getObservations()});
			}
			
		} catch (SQLException e) 
		{
			JOptionPane.showMessageDialog(null, e.getLocalizedMessage());
		} catch (ClassNotFoundException e) 
		{
			JOptionPane.showMessageDialog(null, e.getLocalizedMessage());
		}
		
	}
	
	private void editFamily()
	{
		int selected = tbFamilies.getSelectedRow();
		if(selected<0)
		{
			JOptionPane.showMessageDialog(null, "No row selected");
			return;
		}
		
		Family family = families.get(selected);
		new JNewFamily(family, this);
	}
	
	private void deleteFamily()
	{
		
		try {
			int selected = tbFamilies.getSelectedRow();
			if(selected<0)
			{
				JOptionPane.showMessageDialog(null, "No row selected");
				return;
			}
			
			Family family = families.get(selected);
			if(CFamily.getInstance().delete(family))
			{
				JOptionPane.showMessageDialog(null, "Family deleted");
				this.loadFamilies();
			}
			else
			{
				JOptionPane.showMessageDialog(null, "Error on deleting family");
			}
			
		} catch (SQLException e) 
		{
			JOptionPane.showMessageDialog(null, e.getLocalizedMessage());
		} catch (ClassNotFoundException e) {
			JOptionPane.showMessageDialog(null, e.getLocalizedMessage());
		}
	}
	
	private void initGUI() {
		try {
			this.setVisible(true);
			this.setPreferredSize(new java.awt.Dimension(419, 343));
			this.setLayout(null);
			this.setSize(419, 343);
			{
				lblTitle = new JLabel();
				this.add(lblTitle);
				lblTitle.setText("List Families");
				lblTitle.setBounds(12, 5, 376, 21);
				lblTitle.setHorizontalAlignment(SwingConstants.CENTER);
			}
			{
				jScrollPane1 = new JScrollPane();
				this.add(jScrollPane1);
				jScrollPane1.setBounds(12, 44, 376, 173);
				{
					tbFamiliesModel = 
							new DefaultTableModel(
									new String[][] { },
									new String[] { "Name", "Description", "Observations" });
					tbFamilies = new JTable();
					jScrollPane1.setViewportView(tbFamilies);
					tbFamilies.setModel(tbFamiliesModel);
					tbFamilies.setBounds(12, 44, 376, 173);
				}
			}
			{
				bttEdit = new JButton();
				this.add(bttEdit);
				bttEdit.setText("Edit");
				bttEdit.setBounds(12, 237, 176, 28);
			}
			{
				bttDelete = new JButton();
				this.add(bttDelete);
				bttDelete.setText("Delete");
				bttDelete.setBounds(213, 237, 176, 28);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}
